<template>
	<div class="app-container">
		<el-form
			:model="queryParams"
			ref="queryRef"
			:inline="true"
			v-show="showSearch"
			label-width="90px"
		>
			<el-form-item label="数据项名称" prop="itemName">
				<el-input
					v-model="queryParams.itemName"
					placeholder="请输入数据项名称"
					clearable
					@keyup.enter="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="关联模块" prop="relateModule">
				<el-input
					v-model="queryParams.relateModule"
					placeholder="请输入关联模块"
					clearable
					@keyup.enter="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="数据项类型" prop="itemType" style="width: 300px">
				<el-select
					v-model="queryParams.itemType"
					placeholder="请选择数据项类型"
					clearable
				>
					<el-option
						v-for="dict in operation_shift_data_item_select_type"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="是否必填" prop="isRequired" style="width: 220px">
				<el-select
					v-model="queryParams.isRequired"
					placeholder="请选择是否必填"
					clearable
				>
					<el-option label="必填" value="1" />
					<el-option label="选填" value="0" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery"
					>搜索</el-button
				>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					plain
					icon="Plus"
					@click="handleAdd"
					v-hasPermi="['web:item:add']"
					>新增</el-button
				>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					plain
					icon="Edit"
					:disabled="single"
					@click="handleUpdate"
					v-hasPermi="['web:item:edit']"
					>修改</el-button
				>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					plain
					icon="Delete"
					:disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['web:item:remove']"
					>删除</el-button
				>
			</el-col>
			<right-toolbar
				v-model:showSearch="showSearch"
				@queryTable="getList"
			></right-toolbar>
		</el-row>

		<el-table
			v-loading="loading"
			:data="itemList"
			@selection-change="handleSelectionChange"
		>
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="数据项名称" align="center" prop="itemName" />
			<el-table-column label="关联模块" align="center" prop="relateModule" />
			<el-table-column label="数据项类型" align="center" prop="itemType">
				<template #default="scope">
					<dict-tag
						:options="operation_shift_data_item_select_type"
						:value="scope.row.itemType"
					/>
				</template>
			</el-table-column>
			<el-table-column label="下拉选项值" align="center" prop="dropOptions" />
			<el-table-column label="单位" align="center" prop="unit" />
			<el-table-column label="是否必填" align="center" prop="isRequired">
				<template #default="scope">
					<el-tag :type="scope.row.isRequired === 1 ? 'success' : 'info'">
						{{ scope.row.isRequired === 1 ? "必填" : "选填" }}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="显示顺序" align="center" prop="showOrder" />
			<el-table-column label="数据项描述" align="center" prop="itemDesc" />
			<el-table-column
				label="操作"
				align="center"
				class-name="small-padding fixed-width"
			>
				<template #default="scope">
					<el-button
						link
						type="primary"
						icon="Edit"
						@click="handleUpdate(scope.row)"
						v-hasPermi="['web:item:edit']"
						>修改</el-button
					>
					<el-button
						link
						type="primary"
						icon="Delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['web:item:remove']"
						>删除</el-button
					>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total > 0"
			:total="total"
			v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改数据项配置对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="itemRef" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="数据项名称" prop="itemName">
					<el-input v-model="form.itemName" placeholder="请输入数据项名称" />
				</el-form-item>
				<el-form-item label="关联模块" prop="relateModule">
					<el-input v-model="form.relateModule" placeholder="请输入关联模块" />
				</el-form-item>
				<el-form-item label="数据项类型" prop="itemType">
					<el-select v-model="form.itemType" placeholder="请选择数据项类型">
						<el-option
							v-for="dict in operation_shift_data_item_select_type"
							:key="dict.value"
							:label="dict.label"
							:value="dict.value"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="下拉选项值" prop="dropOptions">
					<el-input
						v-model="form.dropOptions"
						placeholder="请输入下拉选项值（多个用逗号分隔）"
					/>
				</el-form-item>
				<el-form-item label="单位" prop="unit">
					<el-input v-model="form.unit" placeholder="请输入单位" />
				</el-form-item>
				<el-form-item label="是否必填" prop="isRequired">
					<el-select v-model="form.isRequired" placeholder="请选择是否必填">
						<el-option label="必填" :value="1" />
						<el-option label="选填" :value="0" />
					</el-select>
				</el-form-item>
				<el-form-item label="显示顺序" prop="showOrder">
					<el-input v-model="form.showOrder" placeholder="请输入显示顺序" />
				</el-form-item>
				<el-form-item label="数据项描述" prop="itemDesc">
					<el-input v-model="form.itemDesc" placeholder="请输入数据项描述" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Item">
	import {
		listItem,
		getItem,
		delItem,
		addItem,
		updateItem,
	} from "@/api/prod_operate/sys_setting/prod_run/handover_data";

	const { proxy } = getCurrentInstance();
	const { operation_shift_data_item_select_type } = proxy.useDict(
		"operation_shift_data_item_select_type"
	);

	const itemList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");

	const data = reactive({
		form: {
			isRequired: 0, // 默认选填
		},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			itemName: null,
			relateModule: null,
			itemType: null,
			dropOptions: null,
			unit: null,
			isRequired: null,
			showOrder: null,
			itemDesc: null,
		},
		rules: {
			itemName: [
				{ required: true, message: "数据项名称不能为空", trigger: "blur" },
			],
			relateModule: [
				{ required: true, message: "关联模块不能为空", trigger: "blur" },
			],
			itemType: [
				{ required: true, message: "数据项类型不能为空", trigger: "change" },
			],
			isRequired: [
				{ required: true, message: "请选择是否必填", trigger: "change" },
			],
			showOrder: [
				{ required: true, message: "显示顺序不能为空", trigger: "blur" },
			],
		},
	});

	const { queryParams, form, rules } = toRefs(data);

	/** 查询数据项配置列表 */
	function getList() {
		loading.value = true;
		listItem(queryParams.value).then((response) => {
			itemList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			itemId: null,
			itemName: null,
			relateModule: null,
			itemType: null,
			dropOptions: null,
			unit: null,
			isRequired: 0, // 重置为字符串类型的0（选填）
			showOrder: null,
			itemDesc: null,
			createTime: null,
		};
		proxy.resetForm("itemRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map((item) => item.itemId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加数据项配置";
	}

	// 修改按钮操作中的回显处理
	function handleUpdate(row) {
		reset();
		const _itemId = row.itemId || ids.value;
		getItem(_itemId).then((response) => {
			const data = response.data;
			// 确保isRequired为数字类型（后端返回可能为字符串）
			data.isRequired = Number(data.isRequired);
			form.value = data;
			open.value = true;
			title.value = "修改数据项配置";
		});
	}
	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["itemRef"].validate((valid) => {
			if (valid) {
				if (form.value.itemId != null) {
					updateItem(form.value).then((response) => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addItem(form.value).then((response) => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const _itemIds = row.itemId || ids.value;
		proxy.$modal
			.confirm('是否确认删除数据项配置编号为"' + _itemIds + '"的数据项？')
			.then(function () {
				return delItem(_itemIds);
			})
			.then(() => {
				getList();
				proxy.$modal.msgSuccess("删除成功");
			})
			.catch(() => {});
	}

	getList();
</script>
